package com.gogowan.agroenterprise.storage;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaScannerConnection;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.gogowan.agroenterprise.R;
import com.gogowan.agroenterprise.model.StoreModel;
import com.gogowan.agroenterprise.utils.Utils;

/**
 * 货位添加完成
 * 
 * @author tjh 2017-01-06
 *
 */
@SuppressLint("NewApi")
public class GoodsAllocationAddFinishActivity extends Activity {
	private StoreModel storeModel;
	private LinearLayout rootLinear;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_goods_allocation_add_finish);

		initValue();
		initView();
	}

	/**
	 * 初始化数据
	 */
	private void initValue() {
		String storeModelString = this.getIntent().getStringExtra(
				"storeModelString");
		storeModel = (StoreModel) Utils.fromJSON(storeModelString,
				StoreModel.class);
	}

	/**
	 * 初始化界面
	 */
	private void initView() {
		WindowManager wm = this.getWindowManager();
		int width = wm.getDefaultDisplay().getWidth();
		rootLinear = (LinearLayout) findViewById(R.id.goods_allocation_add_finish_linear);
		View view;
		LayoutParams para;
		for (int i = 0; i < storeModel.getGoods().getStoreList().size(); i++) {
			view = LayoutInflater.from(this).inflate(
					R.layout.activity_warehouse_add_finish_itme, null);
			view.setId(i);

			final ImageView ewmImage = (ImageView) view
					.findViewById(R.id.warehouse_add_finish_item_ckewm_image);
			para = ewmImage.getLayoutParams();
			para.height = (int) (width * 0.5);
			para.width = (int) (width * 0.5);
			ewmImage.setLayoutParams(para);

			// 彩色二维码生成
			final Bitmap ewmBit = Utils
					.create2DCoderBitmap(storeModel.getGoods().getStoreList()
							.get(i).getEwmUrl(), width, width);
			ewmImage.setImageBitmap(ewmBit);
			final String name = storeModel.getGoods().getStoreList().get(i)
					.getStoreName()
					+ ".png";
			ewmImage.setOnLongClickListener(new View.OnLongClickListener() {

				@Override
				public boolean onLongClick(View v) {
					// TODO Auto-generated method stub
					String pathStr = Utils.saveEWMBitmapToFile(ewmBit, name);
					if (pathStr == "") {
						Toast.makeText(GoodsAllocationAddFinishActivity.this,
								"保存失败", Toast.LENGTH_LONG).show();
					} else {
						MediaScannerConnection
								.scanFile(
										GoodsAllocationAddFinishActivity.this,
										new String[] { pathStr },
										new String[] { "image/jpeg" },
										new MediaScannerConnection.OnScanCompletedListener() {
											@Override
											public void onScanCompleted(
													String path, Uri uri) {
											}
										});
						Toast.makeText(GoodsAllocationAddFinishActivity.this,
								"保存成功：" + pathStr, Toast.LENGTH_LONG).show();
					}
					return false;
				}
			});

			TextView ckmcText = (TextView) view
					.findViewById(R.id.warehouse_add_finish_item_ckewm_text);
			ckmcText.setText(storeModel.getGoods().getStoreList().get(i)
					.getStoreName()
					+ "二维码如下:");
			rootLinear.addView(view);
		}

		ImageButton fhBtn = (ImageButton) findViewById(R.id.goods_allocation_add_finish_fh_btn);
		fhBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						GoodsAllocationAddFinishActivity.this,
						GoodsAllocationAddActivity.class);
				setResult(RESULT_OK, intent);
				GoodsAllocationAddFinishActivity.this.finish();
			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 返回键
			Intent intent = new Intent(GoodsAllocationAddFinishActivity.this,
					GoodsAllocationAddActivity.class);
			setResult(RESULT_OK, intent);
			GoodsAllocationAddFinishActivity.this.finish();
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		storeModel = null;
	}
}
